package admin.controllers;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.CoordinaModel;
import models.OrderModel;
import models.RouteModel;
import models.StaffModel;
import models.StoreItemModel;
import admin.data.CoordinaData;
import admin.data.RouteData;
import admin.data.StaffData;
import admin.data.StoreItemData;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

import data.OrderData;


public class SpecifyCoordinaServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		response.setCharacterEncoding("UTF-8");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll("/admin/coordina/specify/", "");
		
		RouteData routeData = new RouteData();
		List<RouteModel> listRoute = routeData.getListRoute();

		CoordinaModel coordinaModel = new CoordinaModel();
		CoordinaData coordinaData = new CoordinaData();
		coordinaModel = coordinaData.getById(params);
		
		
		StaffData staffData = new StaffData();
		List<StaffModel> listStaff = staffData.getListByRole(3);
		
		request.setAttribute("coordinaModel", coordinaModel);
		request.setAttribute("listRoute", listRoute);
		request.setAttribute("listStaff", listStaff);

		try {
			request.getRequestDispatcher("/admin/pages/specify_coordina.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		String coordinaId = (String) request.getParameter("coordinaId");
		String staffCode = (String) request.getParameter("staffCode");
		if(coordinaId.length()>8&&staffCode.length()>5)
		{
			CoordinaModel coordinaModel = new CoordinaModel();
			CoordinaData coordinaData = new CoordinaData();
			coordinaModel = coordinaData.getById(coordinaId);			
			coordinaModel.setEmployeeId(staffCode);
			coordinaModel.setStatus(1);
			coordinaModel.setCreated(new Date());
			coordinaData.insert(coordinaModel);
			
			String listOrder = coordinaModel.getListOrder();
			String[] list = listOrder.split(",");
			for (int i = 0; i < list.length; i++) {
				if (list[i].length() > 9) {			
					OrderModel orderModel = new OrderModel();
					orderModel = OrderData.getOrder(list[i]);
					orderModel.setStatus(3);
					OrderData.insert(orderModel);
				}
			}
			HttpSession session = request.getSession();
			session.setAttribute("success", "Chỉ định giao nhận thành công !");
		}		
		else
		{
			HttpSession session = request.getSession();
			session.setAttribute("alert", "Chỉ định giao nhận không thành công !");
		}
		response.sendRedirect("/admin/coordina/list/");
	}
}